package test4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-04-04
 * Time: 17:20
 */
import java.util.*;

//https://www.nowcoder.com/practice/4284c8f466814870bae7799a07d49ec8?tpId=122&tqId=33674&ru=/exam/oj
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] arr = new int[m + 1] ;
        Arrays.fill(arr, Integer.MAX_VALUE);
        arr[n] = 0;
        for (int i = n; i < m; i++) {
            if (arr[i] == Integer.MAX_VALUE) {
                continue;
            }
            List<Integer> list = div(i);
            int ls = list.size();
            for (int j = 0; j < ls; j++) {
                int step = i + list.get(j);
                if (step <= m) {
                    arr[step] = Math.min(arr[step], arr[i] + 1);
                }
            }
        }
        if (arr[m] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(arr[m]);
        }
    }


    public static List<Integer> div(int num) {
        int temp = (int) Math.sqrt(num);
        List<Integer> list = new ArrayList<>();
        for (int i = 2; i <= temp; i++) {
            if (num % i == 0) {
                list.add(i);
                if (i != num / i ) {
                    list.add(num / i);
                }
            }
        }
        return list;
    }
}